﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Sample;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using MafMefInterop;
using System.IO;
using System.AddIn.Hosting;

namespace AddInCatalogSample
{
	public class Host
	{
		[ImportMany]
		private IEnumerable<IMessageProvider> providers;

		public void Run()
		{
			Compose();
			providers.ToList().ForEach(p => Console.WriteLine(p.GetMessage()));
		}

		void Compose()
		{
			AggregateCatalog catalog = new AggregateCatalog();

			catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

			catalog.Catalogs.Add(new AddInCatalog()
				.AddContract<IMessageProvider>()
				.SetActivationPolicy(AddInSecurityLevel.Internet)
			);

			CompositionContainer container = new CompositionContainer(catalog);
			CompositionBatch batch = new CompositionBatch();
			batch.AddPart(this);

			container.Compose(batch);
		}
	}
}
